home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.cs.arizona.edu
/
ftp.cs.arizona.edu.tar
/
ftp.cs.arizona.edu
/
icon
/
newsgrp
/
group00b.txt
/
000074_icon-group-sender _Wed Oct 11 14:45:48 2000.msg
< prev
next >
Wrap
Internet Message Format
|
2001-01-03
|
3KB
Return-Path: <icon-group-sender>
Received: (from root@localhost)
by baskerville.CS.Arizona.EDU (8.9.1a/8.9.1) id OAA08433
for icon-group-addresses; Wed, 11 Oct 2000 14:45:38 -0700 (MST)
Message-Id: <200010112145.OAA08433@baskerville.CS.Arizona.EDU>
From: Steve Wampler <swampler@noao.edu>
X-Newsgroups: comp.lang.icon
Subject: Re: can icon do this?
Date: Wed, 11 Oct 2000 14:04:26 -0700
X-Trace: noao.edu 971298269 51656 140.252.38.6 (11 Oct 2000 21:04:29 GMT)
X-Complaints-To: abuse@noao.edu
To: Antonella <gea@earthling.net>
X-Accept-Language: en
To: icon-group@optima.CS.Arizona.EDU
Errors-To: icon-group-errors@optima.CS.Arizona.EDU
Status: RO
Content-Length: 2569
Antonella wrote:
>
> What code inspects one input text file, checks each line for occurrences
> of a string, and if found replaces that string in an otherwise identical
> output file?
There are several parts to this (based on information in a later posting
from Antonella):
(a) given s1, s2, and target, produce source so that every occurrence of
s1 in target has been replaced by s2
(b) handle multiple pairs of s1,s2
(c) figure out how to specify these multiple pairs on the command line.
One solution to (a) is:
procedure mapString(sText, tText, inLine)
outLine := ""
inLine ? {
while outLine ||:= (tab(find(sText)) || tText) do {
move(*sText)
}
outLine ||:= tab(0)
}
return outLine
end
(b) is tricky, but one easy to implement approach is to use
a table mapping each s1 into its matching s2. Assuming
such a table exists, then (a) can be used to help solve (b)
as in:
procedure mapStrings(sMap, inLine)
outLine := inLine
every s1 := key(sMap) do {
outLine := mapString(s1, sMap[s1], outLine)
}
return outLine
end
Now the table has to be built. It's easiest to combine this
with parsing the command-line arguments. If the commandline
contains arguments of the form s1=s2, then (c) is:
procedure buildMap(args)
sMap := table()
every args := !args do {
s1 := s2 := ""
arg ? {
s1 := tab(upto("=")) | next
move(1)
s2 := tab(0)
}
if *s1 = 0 then stop("Cannot use null string as a source string")
sMap[s1] := s2
}
return sMap
end
Putting it altogether is a main procedure that reads from
standard input and writes to standard output:
procedure main(args)
if *args = 0 then stop("Usage: mapFile string1=string2 ...")
sMap := buildMap(args)
every write(mapStrings(sMap, !&input))
end
If this code is in mapFile.icn and is translated into mapFile, then
an example call would be:
mapFile string=fling arg=barge <mapFile.icn >newFile.icn
A potentially more elegant approach (since this one requires multiple
passes over each input lines) is to write a varient of find() that accepts
a set of strings to find and generates the positions of those strings
in increasing order). Anyone up for it? Would it be faster, slower,
or about the same as this approach?
--
Steve Wampler- SOLIS Project, National Solar Observatory
swampler@noao.edu